$('.li').click(function () {
  $(this).toggleClass('active').find('ol').slideToggle(300)
  $(this).siblings().removeClass('active').find('ol').slideUp(300)
})
$('.lli').click(function () {
  $(this).find('ul').slideToggle(300)
  $(this).siblings().find('ul').slideUp(300)
})
$('.bgcode').mouseenter(function () {
  $('.code').toggleClass('active')
})
$('.bgcode').mouseleave(function () {
  $('.code').toggleClass('active')
})
const login = document.querySelector('.login')
const rlogin = document.querySelector('.rlogin')
const exit = document.querySelector('.exit')
const onelogin = document.querySelector('#onelogin')
const twologin = document.querySelector('#twologin')
const id = window.localStorage.getItem('id')
const phone = window.localStorage.getItem('phone')
authentication()
function authentication() {
  console.log(id)
  if (!id) {
    twologin.style.display = 'none'
    onelogin.style.display = 'block'
    return
  } else {
    onelogin.style.display = 'none'
    twologin.style.display = 'block'
    rlogin.innerText = `欢迎 ${phone} 回来`
    exit.innerText = '退出'
  }
}
exit.addEventListener('click', () => {
  window.localStorage.removeItem('id')
  window.localStorage.removeItem('phone')
  window.location.reload()
})

const otal_number_of = document.querySelector('.otal_number_of')
const otal_number_of_two = document.querySelector('.otal_number_of_two')
const total = document.querySelector('.total')
const apply = document.querySelector('.apply')
const sorting_p = document.querySelector('.sorting > p')
const sorting_1 = document.querySelector('.sorting_1')
const sorting_li = document.querySelectorAll('.sorting_1 > li')
const change_the_page = document.querySelector('.change_the_page')
const left = document.querySelector('.change_the_page > .left')
const right = document.querySelector('.change_the_page > .right')
const pop_up = document.querySelector('.pop-up')
const current = document.querySelector('.current')
const bottom = document.querySelector('.bottom')
const new_sorting = document.querySelector('.new_sorting')
const sales_order = document.querySelector('.sales_order')
const low_to_high = document.querySelector('.low_to_high')
const high_to_low = document.querySelector('.high_to_low')
let page = 1
let currentInp = 1
list()
async function list() {
  // const info = JSON.stringify({ p: 2})
  const res1 = await pAjax({
    url: '/fll',
    data: { "p": page },
    method: 'post',
    dataType: 'json'
  })
  console.log(res1)
  const arr = []
  const obj = res1.id_goods
  for (let k in obj) {
    // console.log(obj[k]);
    arr.push(obj[k])
  }
  const infor = {
    id_goods: arr
  }
  const res = await pAjax({
    url: '/fl',
    data: infor,
    method: 'post',
    dataType: 'json'
  })
  console.log(res)

  otal_number_of.innerText = `(${res1.info._total})`
  otal_number_of_two.innerText = `(${res1.info._total})`
  total.innerText = res1.info._page
  let str = ''
  res.data.forEach(function (item) {
    str += `
        <li class="smapply">
          <img class="bgpictur" src="${item.info.image}">
          <p class="text">${item.info.pro_name}</p>
          <div class="smbanner">
            <img width="30px" height="30px" class="smpictur" src="${item.info.image}" alt="">
          </div>
          <p class="price">
            <span class="presentPrice">￥${item.info.price}</span>
            <span class="originalPrice" ${item.info.price !== item.info.market_price ? 'style="opacity: 1"' : 'style="opacity: 0"'}>￥${item.info.market_price}</span>
          </p>
        </li>
    `
  })
  apply.innerHTML = str
  // console.log(res1.info._page)
  let lly = ''
  for (let i = 1; i < res1.info._page + 1; i++) {
    lly += `<option class="pop-up_list" value="${i}">${i}</option>`
  }
  pop_up.innerHTML = lly
  pop_up.value = currentInp
  return res
}
sorting_p.addEventListener('click', () => {
  if (sorting_1.style.display === 'block') {
    sorting_1.style.display = 'none'
  } else {
    sorting_1.style.display = 'block'
  }
})
for (let i = 0; i < sorting_li.length; i++) {
  sorting_li[i].addEventListener('click', () => {
    for (let j = 0; j < sorting_li.length; j++) {
      sorting_li[j].classList.remove('active')
    }
    sorting_li[i].classList.add('active')
  })
}

change_the_page.addEventListener('click', (e) => {
  e = e || window.event
  const target = e.target || e.srcElement
  if (target.className === 'right') {
    currentInp++
    page++
    if (page >= total.innerText) {
      currentInp = total.innerText
      list()
      return
    }
    window.scrollTo(0, 0)
    list()
  }
  if (target.className === 'left') {
    page--
    if (page <= 1) {
      page = 1
      currentInp = 1
    }
    window.scrollTo(0, 0)
    list()
    current.innerText = currentInp
  }
  if (target.className === 'bottom') {
    if (pop_up.style.display === 'none') {
      pop_up.style.display = 'block'
    } else {
      pop_up.style.display = 'none'
    }
  }
})
pop_up.onchange = function () {
  page = this.value
  currentInp = this.value
  window.scrollTo(0, 0)
  list()
}
goods()
async function goods() {
  const res = await list()
  console.log(res)
  sorting_1.addEventListener('click', (e) => {
    e = e || window.event
    const target = e.target || e.srcElement
    if (target.className === 'new_sorting') {

    }
    if (target.className === 'sales_order') {

    }
    if (target.className === 'low_to_high') {
      console.log('afa')
       console.log(res.data.info.price)
    }
    if (target.className === 'high_to_low') {

    }
  })
}
